# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# READ IN OUR DATA -------------------------------------------------------------
# Focus on the age-group-specific estimates across time aggregations

fig_dt <-
    readRDS(
        "~/estimation-output/wealth_effect_estimates_per_adult_total_empl_income_by_age.rds"  #nolint
    )
setnames(fig_dt, "ref_event_time", "event_time")

# Produce graph labels
fig_dt[
    ,
    age_group :=
    factor(
        age_group,
        levels = c("Younger Winners", "Older Winners")
    )
]

fig_dt[, time_window_label := as.character(NA)]
fig_dt[
    event_time == 99, time_window_label := "Avg (+1 to +5)"
]
fig_dt[
    event_time == 97, time_window_label := "Short Run (+1 to +2)"
]
fig_dt[
    event_time == 98, time_window_label := "Long Run (+3 to +5)"
]
fig_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1 to +2)", "Long Run (+3 to +5)") #nolint
        )
]

# Figure B.7 -- wealth effects across time and by age group

# Just to make it readable against to first, dark bar
errorbar_palette <-
    c(
        "#999999",
        "#000000"
    )

figure_b_7 <-
    ggplot(
        data = fig_dt,
        aes(
            x = factor(time_window_label),
            y = (estimate * 100),
            fill = factor(age_group)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se)) * 100),
        ymax = ((estimate + (1.64 * cluster_se)) * 100),
        color = factor(age_group)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 11.5) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = -40, to = 0, by = 5) / 10)) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $100 of Lottery Winnings") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(-4, 0))

ggsave(
    plot = figure_b_7,
    filename = "~/paper/figures/figure-B.7.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_7,
    filename = "~/paper/figures/figure-B.7.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
fig_dt <- NULL
errorbar_palette <- NULL
figure_b_7 <- NULL
rm(fig_dt, errorbar_palette, figure_b_7)